package com.cn.bcyp.api.service;

import java.util.Date;
import java.util.List;

import com.cn.bcyp.api.db.DBOperation;
import com.cn.bcyp.api.domain.Http_Request_Data;
import com.cn.bcyp.api.domain.StatisticsData;
import com.cn.bcyp.api.utils.ExcelUtil;
import com.cn.bcyp.api.utils.HelperUtils;
import com.cn.bcyp.api.verify.VerifyResult;
import com.cn.bcyp.bussiness.AppInterface;

/**
 * 操作excel表格
 */

public class OperationExcel {

	List<Http_Request_Data> data;
	HelperUtils hu = new HelperUtils();
	ExcelUtil eu = new ExcelUtil();
	int colorNum = 2;
	String test_result = "";
	String time = "";
	String result_actual = "";
	String filePath = "";
	String sqlResult = "";
	ReqPort requestPort = new ReqPort();
	VerifyResult verifyResult = new VerifyResult();
	HelperUtils helperUtils = new HelperUtils();
	DBOperation dbOperation = new DBOperation();

	String asser_result = "TRUE";
	String expect_rsult = "";

	private int execgettrue = 0;// 执行get请求成功
	private int execgetfalse = 0;// 执行get请求失败
	private int execposttrue = 0;// 执行post请求成功
	private int execpostfalse = 0;// 执行post请求失败
	private int execyestest = 0;// 执行成功
	private int execnotest = 0;// 执行失败

	public void setExecgettrue(int execgettrue) {
		this.execgettrue = execgettrue;
	}

	public void setExecgetfalse(int execgetfalse) {
		this.execgetfalse = execgetfalse;
	}

	public void setExecposttrue(int execposttrue) {
		this.execposttrue = execposttrue;
	}

	public void setExecpostfalse(int execpostfalse) {
		this.execpostfalse = execpostfalse;
	}

	public void setExecyestest(int execyestest) {
		this.execyestest = execyestest;
	}

	public void setExecnotest(int execnotest) {
		this.execnotest = execnotest;
	}

	public int getExecnotest() {
		return execnotest;
	}

	public int getExecgettrue() {
		return execgettrue;
	}

	public int getExecgetfalse() {
		return execgetfalse;
	}

	public int getExecposttrue() {
		return execposttrue;
	}

	public int getExecpostfalse() {
		return execpostfalse;
	}

	public int getExecyestest() {
		return execyestest;
	}

	private int yesNum__case = 0;
	private int noNum__case = 0;
	private int getNum_true_case = 0;
	private int getNum_false_case = 0;
	private int postNum_true_case = 0;
	private int postNum_false_case = 0;
	private String caseContent = "";

	public String getCaseContent() {
		return caseContent;
	}

	public void setCaseContent(String caseContent) {
		this.caseContent = caseContent;
	}

	public int getYesNum__case() {
		return yesNum__case;
	}

	public void setYesNum__case(int yesNum__case) {
		this.yesNum__case = yesNum__case;
	}

	public int getNoNum__case() {
		return noNum__case;
	}

	public void setNoNum__case(int noNum__case) {
		this.noNum__case = noNum__case;
	}

	public int getGetNum_true_case() {
		return getNum_true_case;
	}

	public void setGetNum_true_case(int getNum_true_case) {
		this.getNum_true_case = getNum_true_case;
	}

	public int getGetNum_false_case() {
		return getNum_false_case;
	}

	public void setGetNum_false_case(int getNum_false_case) {
		this.getNum_false_case = getNum_false_case;
	}

	public int getPostNum_true_case() {
		return postNum_true_case;
	}

	public void setPostNum_true_case(int postNum_true_case) {
		this.postNum_true_case = postNum_true_case;
	}

	public int getPostNum_false_case() {
		return postNum_false_case;
	}

	public void setPostNum_false_case(int postNum_false_case) {
		this.postNum_false_case = postNum_false_case;
	}

	String statistics = "";
	String req_host = "";
	String req_interface = "";

	String reqType = "";// 请求方式
	String isSuccess = "";// 是否成功
	StatisticsData sd = new StatisticsData();

	Long startTime1;
	Long endTime1;

	Double alltime = 0.000;
	Double maxtime;

	/**
	 * 读取excel表数据
	 * @param fileName
	 *            文件名
	 */
	public void readExcel(Object fileName) {
		maxtime = 0.000;
		filePath = (String) fileName;
		data = eu.readExcel(filePath);
		if (data != null) {
			for (int i = 0; i <= data.size() - 1; i++) {
				statistics = data.get(i).getInterfac_classification();
				req_host = data.get(i).getReq_host();
				req_interface = data.get(i).getReq_interface();

				if (data.get(i).getTest_is_exec().equals("YES")) {
					execyestest++;
					setYesNum__case(1);

					reqType = data.get(i).getReq_type();

					if (data.get(i).getReq_type().equals("POST")) {
						execPostData(i);
					} else if (data.get(i).getReq_type().equals("GET")) {
						execGetData(i);
					} else {
						System.out.println("Req_type值只能是POST或GET！");
					}
				} else if (data.get(i).getTest_is_exec().equals("NO")) {
					execnotest++;
					setNoNum__case(1);
				} else {
					System.out.println("Test_is_exec值只能是YES或NO！");
				}

				caseContent += getCaseStatisticsData();
				setCaseContent(caseContent);
				setYesNum__case(0);
				setNoNum__case(0);
				setGetNum_true_case(0);
				setGetNum_false_case(0);
				setPostNum_true_case(0);
				setPostNum_false_case(0);
			}
			sd.setAverageTime(Double.parseDouble(hu.formatDouble3(alltime / data.size())));
		}
		alltime = 0.0;
	}

	/**
	 * 执行get请求
	 * 
	 * @param i
	 */
	private void execGetData(int i) {
		data.get(i).setTest_exectime(hu.getNowData(new Date()));
		String url = data.get(i).getReq_host() + data.get(i).getReq_interface() + data.get(i).getReq_data();
		String cookie = AppInterface.getCookie(data.get(i).getReq_phone());
		// 计算每个接口响应时间
		startTime1 = System.currentTimeMillis();
		result_actual = requestPort.reqTypeGet(url, cookie);
		endTime1 = System.currentTimeMillis();
		Double rt = (endTime1 - startTime1) / 1000.000;
		sd.setResponseTime(rt);

		// 判断接口最大响应时间
		if (maxtime < rt) {
			maxtime = rt;
			sd.setMaxTime(rt);
		}

		if (data.get(i).getReq_sql().toString().length() > 0) {
			expect_rsult = assertMethod(data.get(i).getReq_sql().trim());
		} else {
			test_result = verifyResult.assertResult(data.get(i).getResult_expected(), result_actual);
		}

		if (test_result.equals("TRUE")) {
			execgettrue++;
			setGetNum_true_case(1);
		} else if (test_result.equals("FALSE")) {
			execgetfalse++;
			setGetNum_false_case(1);
		}
		updateExcelData(i, result_actual, test_result, expect_rsult);
	}

	/**
	 * sql断言
	 * 
	 * @param sql
	 * @return
	 */
	private String assertMethod(String sql) {
		if (sql.contains(";")) {
			String[] sqls = sql.split(";");
			for (int j = 0; j < sqls.length; j++) {
				sqlResult = dbOperation.getSQLResult(sqls[j]);
				asser_result = verifyResult.assertResult(sqlResult, result_actual);
				expect_rsult += sqlResult + ";";
			}
			if (asser_result.contains("FALSE")) {
				test_result = "FALSE";
			}
		} else {
			sqlResult = dbOperation.getSQLResult(sql);
			test_result = verifyResult.assertResult(sqlResult, result_actual);
			expect_rsult = sqlResult;
		}
		return expect_rsult;
	}

	/**
	 * 执行post数据
	 * 
	 * @param i
	 */
	private void execPostData(int i) {
		data.get(i).setTest_exectime(hu.getNowData(new Date()));

		String url = data.get(i).getReq_host() + data.get(i).getReq_interface() + data.get(i).getReq_data();
		String reqdata = data.get(i).getReq_data();
		String cookie = AppInterface.getCookie(data.get(i).getReq_phone());

		// 计算每个接口响应时间
		startTime1 = System.currentTimeMillis();
		result_actual = requestPort.reqTypeGet(url, cookie);
		endTime1 = System.currentTimeMillis();
		result_actual = requestPort.reqTypePost(url, reqdata, cookie);
		Double rt = (endTime1 - startTime1) / 1000.000;
		sd.setResponseTime(rt);

		// 判断接口最大响应时间
		if (maxtime < rt) {
			maxtime = rt;
			sd.setMaxTime(rt);
		}

		if (data.get(i).getReq_sql().toString().length() > 0) {
			expect_rsult = assertMethod(data.get(i).getReq_sql().trim());
		} else {
			test_result = verifyResult.assertResult(data.get(i).getResult_expected(), result_actual);
		}

		if (test_result.equals("TRUE")) {
			execposttrue++;
			setPostNum_true_case(1);
		} else if (test_result.equals("FALSE")) {
			execpostfalse++;
			setPostNum_false_case(1);
		}
		updateExcelData(i, result_actual, test_result, expect_rsult);
	}

	/**
	 * 更新excel数据
	 * 
	 * @param i
	 * @param result_actual
	 */
	private void updateExcelData(int i, String result_actual, String test_result, String expect_value) {
		if (test_result.equals("TRUE")) {
			colorNum = 3;
		} else if (test_result.equals("FALSE")) {
			colorNum = 4;
		}
		try {
			if ("".equals(expect_value) || expect_value.length() <= 0) {
				eu.updateExcel(filePath, 11, i + 1, result_actual, colorNum);
				eu.updateExcel(filePath, 12, i + 1, test_result, colorNum);
				eu.updateExcel(filePath, 13, i + 1, data.get(i).getTest_exectime(), 2);
			} else {
				eu.updateExcel(filePath, 10, i + 1, expect_value, 5);
				eu.updateExcel(filePath, 11, i + 1, result_actual, colorNum);
				eu.updateExcel(filePath, 12, i + 1, test_result, colorNum);
				eu.updateExcel(filePath, 13, i + 1, data.get(i).getTest_exectime(), 2);
			}

		} catch (Exception e) {
			throw new RuntimeException(e);
		}
		expect_rsult = "";
	}

	/**
	 * 获取不同接口统计信息
	 * 
	 * @return
	 */
	public String getStatisticsData() {

		String successRate = hu.num_percent(getExecgettrue() + getExecposttrue(), getExecyestest());
		String failRate = hu.num_percent(getExecgetfalse() + getExecpostfalse(), getExecyestest());

		String data = "";
		data = "<tr>"
				+ "<td align='center' style='background:#99CCFF'>" + statistics + "</td>"
				+ "<td style='background:#99CCFF'>" + req_host + "</td>"
				+ "<td align='center' style='background:#99CCFF'>" + (getExecyestest() + getExecnotest()) + "</td>"
				+ "<td align='center' style='background:#99CCFF'>" + getExecyestest() + "</td>"
				+ "<td align='center' style='background:#99CCFF'>" + getExecnotest() + "</td>"

				+ "<td align='center' style='background:#99CCFF'>" + "--" + "</td>"
				+ "<td align='center' style='background:#99CCFF'>" + "--" + "</td>"

				+ "<td align='center' style='background:#99CCFF'>" + "--" + "</td>"
				+ "<td align='center' style='background:#99CCFF'><font color='red'>" + sd.getAverageTime()
				+ "s</font></td>" + "<td align='center' style='background:#99CCFF'><font color='red'>" + sd.getMaxTime()
				+ "s</font></td>"

				+ "<td align='center' style='background:#99CCFF'><font color='red'>" + successRate + "</font></td>"
				+ "<td align='center' style='background:#99CCFF'><font color='red'>" + failRate + "</font></td>"

				+ "</tr>" + getCaseContent();
		setCaseContent("");
		return data;
	}

	/**
	 * 获取用例统计信息
	 * 
	 * @return
	 */
	public String getCaseStatisticsData() {
		String data = "";
		if (getGetNum_true_case() > 0 || getPostNum_true_case() > 0) {
			isSuccess = "成功";
		} else {
			isSuccess = "失败";
		}
		alltime += sd.getResponseTime();

		data = "<tr>" + "<td></td>" + "<td>" + req_interface.substring(0, req_interface.length() - 1) + "</td>"
				+ "<td align='center'>1</td>" + "<td align='center'>" + getYesNum__case() + "</td>"
				+ "<td align='center'>" + getNoNum__case() + "</td>"

				+ "<td align='center'>" + reqType + "</td>" + "<td align='center'>" + isSuccess + "</td>"

				+ "<td align='center'><font color='green'>" + sd.getResponseTime() + "s</font></td>"
				+ "<td align='center'></td>" + "<td align='center'></td>"

				+ "<td align='center'></td>" + "<td align='center'></td>"

				+ "</tr>";
		return data;
	}

}
